<script setup lang="ts">
import {useDataPublicStore} from '@/stores/dataPublicStore.ts';
import {onMounted, ref} from "vue";
import {saveOneEquipmentTransferAPI} from '@/apis/equipmentTransfer.ts'
import {useLocationStore} from '@/stores/locationStore.ts'
import CameraCapture from "@/views/PublicCom/cameraCapture.vue";
import Upload from "@/views/PublicCom/ImageFilesUpload.vue";
import router from "@/router";
import {useRoute} from "vue-router";
import {useEquipmentStore} from '@/stores/equipmentStore.ts';
const route=useRoute();
const equipmentStore=useEquipmentStore();
const dataPublicStore =useDataPublicStore();
const locationStore=useLocationStore();
const current=ref(1);
const pageSize=ref(100);

locationStore.getLocationInfo(current.value,pageSize.value);
const location=ref('');
const description=ref('');


const oneEquipment=ref({});
onMounted(async ()=>{
  // if (!eqObjStore.id){
  //   router.push({path:'/home/equipment/equipmentTransfer'})
  // }

  console.log(route.params.id);
  if (route.params.id===null||route.params.id===undefined){
    await router.push({path:'/home/equipment/equipmentTransfer'})
  }
  else{
    await equipmentStore.getOneByIdStore(route.params.id);
    oneEquipment.value=equipmentStore.equipmentData;
  }


})

// // 时间戳变成真实时间
// function getDate(time: number) {
//   let date = new Date(time);
//   let Y=date.getFullYear()+'年';
//   let M=(date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '月';
//   let D=date.getDate()+'日';
//   let h=date.getHours()+'时';
//   let m=date.getMinutes()+'分';
//   return Y+M+D+h+m;
// }


// 显示进度条配合函数
const format=(percentage:number)=>{
  return percentage===100?'100%':`${percentage}`
}

const disableSwitch=ref(false);

const submitSaveOneTransfer=async ()=>{
  const formData=new FormData();
  formData.append("id", oneEquipment.value.id);
  formData.append('description',description.value);
  formData.append('receivingProject',location.value);
  if (dataPublicStore.photoFileList.length>0){
    dataPublicStore.photoFileList.forEach((item:File)=>{
      formData.append('photoFiles',item);
    })
  }

  if (dataPublicStore.uploadFileList.length>0){
    dataPublicStore.uploadFileList.forEach((item:File)=>{
      formData.append('photoFiles',item);
    })
  }
  disableSwitch.value=true;
  await saveOneEquipmentTransferAPI(formData);
  dataPublicStore.setPhotoFiles([]);
  dataPublicStore.setUploadFileList([]);
  dataPublicStore.setOnUploadProgress(0);
  dataPublicStore.saveDataObject({});
  await router.push({path: '/home/equipment/equipmentTransfer'})
}

</script>

<template>
  <!--  进度条-->
  <el-progress :percentage="dataPublicStore.onUploadProgress" :format="format"    :stroke-width="5" v-show="dataPublicStore.onUploadProgress" style="margin: 0;padding: 0" ></el-progress>
  <!-- 面包屑 -->
  <div class="bread-container">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: `/` }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:`/home/equipment`}" >设备主页</el-breadcrumb-item>
      <el-breadcrumb-item  >设备转场</el-breadcrumb-item>
    </el-breadcrumb>
  </div>

  <div class="divTable">
    <table class="table">
      <tbody>
      <tr>
        <td class="tableTitle" colspan="4" >设    备    详    细    信    息</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF;color: #242424;font-family: 微软雅黑,serif">设备名称</td>
        <td style="width: 350px;height: 15px;   text-align: center  ">{{oneEquipment.eqName}}</td>
        <td style="width: 250px;height: 15px; text-align: center ;background: #EFF6FF"  >设备品牌</td>
        <td style="width: 350px;height: 15px ; text-align: center   ">{{oneEquipment.brand}}</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">设备型号</td>
        <td style="width: 350px;height: 15px;text-align: center  ">{{oneEquipment.model}}</td>
        <td style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >设备编码</td>
        <td style="width: 350px;height: 15px;text-align: center   ">{{oneEquipment.encode}}</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">设备数量单位</td>
        <td style="width: 350px;height: 15px;text-align: center  ">{{oneEquipment.pieces}}</td>
        <td style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >设备数量</td>
        <td style="width: 350px;height: 15px;text-align: center   ">{{oneEquipment.number}}</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">设备类型</td>
        <td style="width: 350px;height: 15px;text-align: center  ">{{oneEquipment.typeName}}</td>
        <td style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >设备所在地</td>
        <td style="width: 350px;height: 15px;text-align: center   ">{{oneEquipment.location}}</td>
      </tr>
      <tr>
        <td style="padding: 0;margin: 0">设备转场目的地</td>
        <td colspan="3">
          <el-select
              v-model="location"
              clearable
              placeholder="Select"
              style="padding: 0;margin: 0"

          >
            <el-option
                v-for="(item,index) in locationStore.locationInfo"
                :key="index"
                :label="item.locationAddress"
                :value="item.locationAddress"
            />
          </el-select>
        </td>

      </tr>
      <tr>
        <td  style="width: 250px;height: 30px;text-align: center;background: #EFF6FF">备注</td>
        <td colspan="3" style="width: 850px;height: 15px;text-align: center">
          <el-input  v-model="description" />
        </td>
      </tr>
<!--      <tr>-->
<!--        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">创建人</td>-->
<!--        <td style="width: 350px;height: 15px;text-align: center  ">{{oneEquipment.createBy}}</td>-->
<!--        <td style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >最后修改人</td>-->
<!--        <td style="width: 350px;height: 15px;text-align: center   ">{{oneEquipment.lastUpdateBy}}</td>-->
<!--      </tr>-->
<!--      <tr>-->
<!--        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">创建时间</td>-->
<!--        <td style="width: 350px;height: 15px;text-align: center  ">{{getDate(oneEquipment.createTime)}}</td>-->
<!--        <td style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >最后修改时间</td>-->
<!--        <td style="width: 350px;height: 15px;text-align: center   ">{{ getDate(oneEquipment.updateTime)}}</td>-->
<!--      </tr>-->
<!--      <tr>-->
<!--        <td colspan="4" style="width: 250px;height: 15px;text-align: center;background: #EFF6FF"></td>-->
<!--      </tr>-->

      <tr>
        <td colspan="4" style="width: 250px;height: 15px;text-align: center;background: #EFF6FF"><h2>附 件 资 料 上 传</h2></td>
      </tr>
      <tr>
        <td colspan="4">
          <camera-capture/>
        </td>
      </tr>
      <tr>
        <td colspan="4">
          <upload/>
        </td>
      </tr>
      <tr>
        <td colspan="4" style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">
          <el-button
              type="success"
              size="large"
              @click="submitSaveOneTransfer"
          >
            确定
          </el-button>
        </td>
      </tr>
      </tbody>
    </table>
  </div>
</template>


<style scoped lang="scss">
.bread-container {
  padding: 25px 10px;
  margin-left: 10px;
}
.divTable {
  position: absolute;
  top: 150px;
  bottom: 0;
  left: 300px;
  right: 0;
  margin: auto;
}


table, td,tr{
  border: 1px solid #44a8da;
  text-align: center;
  border-spacing: 0;
  border-collapse: collapse;
  height: 45px;
}

.tableTitle{

  border-right: 0 none;
  border-left: 0 none;
  text-align: center;
  font-size: large;
  font-weight: bold;
}

.demo-image__error .image-slot {
  font-size: 30px;
}
.demo-image__error .image-slot .el-icon {
  font-size: 30px;
}
.demo-image__error .el-image {
  width: 100%;
  height: 200px;
}

</style>